home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / pascal / tch_tpas.zip / TL06.TXT < prev    next >
Text File  |  1986-04-05  |  4KB  |  125 lines

  1. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 25
  2.  
  3.  
  4. TURBO-LESSON 6:  CONDITIONAL PROCESSING
  5.  
  6. OBJECTIVES - You will learn, in this lesson, about:
  7.  
  8. 1.  Selection structures used for conditional processing
  9. 2.  IF statement  (one-way selection)
  10. 3.  IF statement  (two-way selection)
  11.  
  12.  
  13. 1.  Selection structures used for conditional processing.
  14.  
  15. There are three types of statement sequencing used in PASCAL:
  16.  
  17.    (1)  SIMPLE SEQUENCE.  One statement follows another with no 
  18. branching.  
  19.  
  20.    (2)  SELECTION STRUCTURES.  Based on a condition, supplied by 
  21. the programmer, the next statement to execute is chosen from two 
  22. alternatives (IF statement) or chosen from many alternatives 
  23. (CASE statement).  
  24.  
  25.    (3)  REPETITION STRUCTURES.  A group of program statements may 
  26. be repeated more than once, dependent on a condition supplied by 
  27. the programmer.  The repetition statements are WHILE, REPEAT, and 
  28. FOR, included in later lessons.
  29.  
  30. The Selection statement, IF .. THEN .. ELSE, is illustrated in 
  31. this lesson,  the CASE statement appears in a later lesson.
  32. î
  33. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 26
  34.  
  35.  
  36. 2.  IF statement (one-way selection).
  37.  
  38. The one-way IF is really a special case of the two-way IF, where 
  39. the second alternative is to do nothing, just go on to the next 
  40. statement.  The form of the IF statement is:
  41.  
  42. IF condition THEN statement;
  43.  
  44. The condition is an expression or comparison which the computer 
  45. can evaluate as TRUE or FALSE.  Examples of a condition:
  46.  
  47.    7 < 10        TRUE
  48.    I < 10        TRUE, if the memory location named I holds a 
  49.                        value less than 10, otherwise FALSE.
  50.    NOT(7 < 10)   FALSE (7 < 10 is TRUE, but NOT reverses the 
  51.                        value to FALSE).
  52.  
  53. ##### DO:
  54.  
  55. Look at PROG6.  
  56.  
  57. The first IF statement is a one-way selection.  If the condition 
  58. is true, the WriteLn statement will be executed.  If the 
  59. condition is false, the WriteLn will be ignored.  
  60.  
  61. Run the program using 0 for the no of computers owned.
  62.  
  63. Run it again with 1 for the no of computers owned.  
  64.  
  65. The "No Computer!" message should print for 0 computers owned, 
  66. but not print for 1 computer owned.  
  67.  
  68. ##### DO:
  69.  
  70. Examine the last IF in the program.  
  71.  
  72. This is a one-way IF with a slightly more complicated condition.  
  73. The condition contains an integer expression, (Want - Have).  The 
  74. computer first evaluates the integer expression to get a number 
  75. to compare with the 2 on the right side of the ">".  
  76.  
  77. ##### DO:
  78.  
  79. Run the program several times with different input to see the 
  80. effect of this IF.
  81.  
  82. Also note the misspelled "Aren''t" in the message.  The double 
  83. apostrophe is used in the message to represent a single 
  84. apostrophe.  If a single apostrophe were used, it would appear to 
  85. be the end of the message.  
  86. î
  87. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 27
  88.  
  89.  
  90. 3.  IF statement (Two-way selection).
  91.  
  92. The two-way IF causes one of two alternative statements to be 
  93. executed, based on whether the condition is TRUE or FALSE.  The 
  94. form of the statement is:
  95.  
  96. IF condition THEN Statement_1 ELSE Statement_2;
  97.  
  98. If the condition is TRUE, the statement following the THEN is 
  99. executed.  If the condition is FALSE, the statement following the 
  100. ELSE is executed. 
  101.  
  102. ##### DO:
  103.  
  104. Look at the second IF in PROG6.  
  105.  
  106. This is a two-way IF.  A congratulations message is printed if 
  107. the condition is true,  condolences if false.  
  108.  
  109. The condition is (Have >= Want).  This condition is true if the 
  110. number you enter for computers owned is greater than or equal to 
  111. the number you enter for computers you would like to have.
  112.  
  113. ##### DO:
  114.  
  115. Run the program several times, experimenting with various input 
  116. values.  
  117.  
  118. ##### DO:
  119.  
  120. Try your hand at writing an IF statement to do the following:
  121.  
  122. If the number of computers owned is more than the number of 
  123. computers wanted, print a message 'Send extra computers to 
  124. (put your name here?) '.
  125. î